programming4us
           
 
 
Programming

iPhone Programming : Connecting to the Network - Sending Email

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/12/2011 9:14:07 AM
The MFMailComposeViewController class provides access to the same interface used by the Mail client to edit and send an email. The most common way to present this interface is to do so modally using the presentModalViewController:animated: method, just as we did in the preceding section to create a reusable web view class.

We can therefore reuse our Prototype application code from the preceding section to demonstrate how the mail composer works; we’ll just drop in a class that displays the mail interface instead of the web interface. Open the Finder and navigate to the location where you saved the Prototype project. Right-click on the folder containing the project files and select Duplicate; a folder called Prototype copy will be created containing a duplicate of our project. Rename the folder Prototype2, and then open the new (duplicate) project inside Xcode and use the ProjectRename tool to rename the project itself.

Next, prune back the code:

  1. Open the copy of the project in Xcode and delete the WebViewController.h, WebViewController.m, and WebView.xib files by right-clicking on each file in the Groups & Files pane and selecting Delete from the pop-up menu. When prompted, click Also Move to Trash. If you moved WebView.xib into your Resources folder with the rest of the NIBs, look for it there.

  2. Now click on the PrototypeViewController.m file to open it in the editor. Delete the line where you import the WebViewController.h file and delete all the code in the pushedGo: method, but not the method itself.

At this point, we have just the stub of the application, with that Go! button and associated pushedGo: method we can use to trigger the display of our mail composer view. So, let’s write the code to do that now.

The first thing we need to do is add the MessageUI.framework framework to the project. As you did earlier for the SystemConfiguration.framework, right-click on the Frameworks group and select AddExisting Frameworks. Then select the MessageUI.framework from the list presented in the framework selection pop-up window.


Warning:

If you have upgraded your Xcode (and iPhone SDK) distribution in the middle of developing a project, MessageUI.framework may not show up in the list of frameworks presented to you by Xcode in the framework selection pop up. If this turns out to be the case, you may be able to resolve the problem by opening the Targets group in the Groups & Files pane in Xcode, right-clicking on the application’s target, and selecting Get Info. Navigate to the Build pane of the Target Info window and set the Base SDK of your project to the SDK you currently have installed (rather than the SDK with which you initially developed the project).


We’re going to present our mail composer view when the Go! button is clicked using our pushedGo: method. However, before we do, we need to see if the device is even configured to send email, using the canSendMail: class method. If it isn’t, we need to inform the user that the device isn’t able to send mail. When writing a real application that relies on email being available, you might want to do this check when the application starts inside your application delegate, and then either inform the user that there is a problem or disable the parts of your application that depend on it being able to send mail. Add the following code to the pushedGo: method in PrototypeViewController.m:

-(IBAction) pushedGo:(id)sender {
if (![MFMailComposeViewController canSendMail]) {
NSString *errorTitle = @"Error";
NSString *errorString =
@"This device is not configured to send email.";
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:errorTitle
message:errorString
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[errorView show];
[errorView release];
} else {
MFMailComposeViewController *mailView =
[[[MFMailComposeViewController alloc] init] autorelease];
mailView.mailComposeDelegate = self;
[mailView setSubject:@"Test"];
[mailView setMessageBody:@"This is a test message" isHTML:NO];
[self presentModalViewController:mailView animated:YES];
}
}

The mail composer view won’t dismiss itself when the user clicks the Send or Cancel button. We need to know when it is dismissed by the user; for that to happen we must implement the MFMailComposeViewControllerDelegate protocol. We therefore need to import the framework headers into the PrototypeViewController.h interface file, which we do by importing the MessageUI.h header file:
#import <MessageUI/MessageUI.h>

We also have to declare our PrototypeViewController as a delegate class for the mail view by changing the declaration in PrototypeViewController.h, as shown here:

@interface PrototypeViewController : UIViewController
<MFMailComposeViewControllerDelegate> {
... no changes to the code in here ...
}

The delegate protocol implements only one method, which dismisses the view controller and handles any errors: the mailComposeController:didFinishWithRe⁠sult:error: method. Let’s implement that now as part of our PrototypeViewController class. Add the following method to PrototypeViewController.m:

-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
if (error) {
NSString *errorTitle = @"Mail Error";
NSString *errorDescription = [error localizedDescription];
UIAlertView *errorView = [[UIAlertView alloc]
initWithTitle:errorTitle
message:errorDescription
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[errorView show];
[errorView release];

} else {
// Add code here to handle the MFMailComposeResult
}

[controller dismissModalViewControllerAnimated:YES];
}

Before we discuss how to handle the MFMailComposeResult, let’s test our code. Click the Build and Go button on the Xcode toolbar to compile and start the application in iPhone Simulator. Once the application opens, click the Go! button. If all goes well, you should see something very similar to Figure 1.

Now that the application is working, let’s handle that MFMailComposeResult. The simplest way to illustrate how to handle the result is to add a label to the PrototypeViewController NIB file, and display the result returned by the mail composer view there.

The first thing you need to do is to add a UILabel to the PrototypeViewController.h interface file and declare it as an IBOutlet. Add the line shown in bold:

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface PrototypeViewController : UIViewController
<MFMailComposeViewControllerDelegate> {
IBOutlet UIButton *goButton;
IBOutlet UILabel *resultLabel;
}

-(IBAction) pushedGo:(id)sender;

@end

Figure 1. The MFMailMailComposeViewController


Remember that now we’ve declared the label variable, so we also need to release it inside the dealloc: method. Add the following to the dealloc: method in PrototypeViewController.m:

[resultLabel release];

We also need to open the PrototypeViewController.xib file in Interface Builder and add the label. Open the NIB file and then drag and drop a label (UILabel) from the Library window onto the view. Now right-click on File’s Owner and connect the resultLabel outlet to the new UILabel. Make sure you save your changes to the NIB file, and then return to Xcode.

Now we can use the label to display the results. Inside the mail composer delegate method, replace the line that reads // Add code here to handle the MFMailComposeRe⁠⁠sult with the following code:

NSString *string;
switch (result) {
case MFMailComposeResultSent:
string = @"Mail sent.";
break;
case MFMailComposeResultSaved:
string = @"Mail saved.";
break;
case MFMailComposeResultCancelled:
string = @"Mail cancelled.";
break;
case MFMailComposeResultFailed:
string = @"Mail failed.";
break;
default:
string = @"Unknown";
break;
}
resultLabel.text = string;

The switch statement we just added enumerates the possible results, and then sets the label string to a human-readable result. We’re done. If you build the application again and send an email from the composer view, you should see something very much like Figure 2.

Figure 2. We successfully sent the mail message


Attaching an Image to a Mail Message

You can attach an image to your mail message by using the addAttachmentData:mimeType:Filename: method. This should be called before displaying the mail composer interface, directly after the call to the setMessageBody:isHTML: method. You should not call this method after displaying the composer interface to the user.

If necessary, you can change the image type using the UIImageJPEGRepresentation() or UIImagePNGRepresentation() UIKit function, as shown here:

UIImage *image = [UIImage imageNamed:@"Attachment.png"];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
[mailView addAttachmentData:data mimeType:@"image/jpeg"
fileName:@"Picture.jpeg"];

This example will look for Attachment.png at the root of the application bundle (to put a file there, drag it into the top level of the Groups & Files pane), convert it to a JPEG, and attach it under the filename Picture.jpeg.

Other -----------------
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Check Results
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Read and Write Files
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Get Dates and Times
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Work with Text
- A Technical Overview of the Mobile Web : THE TECHNICAL CHALLENGES OF MOBILE DEVICES (part 2)
- A Technical Overview of the Mobile Web : THE TECHNICAL CHALLENGES OF MOBILE DEVICES (part 1) - Physical Constraints
- Parallel Programming with Microsoft Visual Studio 2010 : Task Parallelism - Unhandled Exceptions in Tasks
- Parallel Programming with Microsoft Visual Studio 2010 : Introduction to Parallel Tasks
- jQuery 1.3 : DOM Manipulation - Moving elements
- .NET Debugging : Introduction to the Tools - .NET 2.0—Redistributable & .NET 2.0—SDK
- .NET Debugging : Managed Heap and Garbage Collection
- Context and Interception : Custom Component Services (part 3) - The Transaction Management Service
- Context and Interception : Custom Component Services (part 2) - The Logbook Service
- Context and Interception : Custom Component Services (part 1) - Building a Custom Context Attribute & Installing a Custom Message Sink
- Software Testing with Visual Studio Team System 2008 : Data-driven unit testing
- Software Testing with Visual Studio Team System 2008 : Unit testing an ASP.NET application
- Microsoft Enterprise Library : Error Management Made Exceptionally Easy - Replacing an Exception & Logging an Exception
- Microsoft Enterprise Library : Error Management Made Exceptionally Easy - Diving in with a Simple Example
- iPhone Programming : Connecting to the Network - Embedding a Web Browser in Your App
- iPhone Programming : Connecting to the Network - Detecting Network Status
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us